#include "NeoToggleButtonPrivate.h"
#include "NeoIcon.h"
#include "NeoToggleButton.h"
#include <QParallelAnimationGroup>
#include <QPropertyAnimation>

NeoToggleButtonPrivate::NeoToggleButtonPrivate(QObject *parent)
    : QObject(parent),
      pChecked(false),
      pBackgroundCacheColor(Qt::lightGray),
      pIsAnimationEnabled(true),
      pIcon(NeoIconType::FontAwesomeSolid::None),
      pNormalBackgroundColor(Qt::lightGray),
      pCheckedBackgroundColor(Qt::blue),
      pNormalForegroundColor(Qt::black),
      pCheckedForegroundColor(Qt::white),
      pBackgroundColorAnimation(new QPropertyAnimation(this, "pBackgroundCacheColor", this)),
      pForegroundColorAnimation(new QPropertyAnimation(this, "pTextCacheColor", this)),
      pAnimationGroup(new QParallelAnimationGroup(this))
{
    pBackgroundColorAnimation->setDuration(300);
    pBackgroundColorAnimation->setEasingCurve(QEasingCurve::InOutQuad);
    pForegroundColorAnimation->setDuration(300);
    pForegroundColorAnimation->setEasingCurve(QEasingCurve::InOutQuad);
    pAnimationGroup->addAnimation(pBackgroundColorAnimation);
    pAnimationGroup->addAnimation(pForegroundColorAnimation);
}

NeoToggleButtonPrivate::~NeoToggleButtonPrivate() = default;

void NeoToggleButtonPrivate::startColorAnimation()
{
    NEO_PRIVATE_POINT(NeoToggleButton)
    if (pAnimationGroup->state() != QAbstractAnimation::Running)
    {
        pBackgroundColorAnimation->setStartValue(pBackgroundCacheColor);
        pBackgroundColorAnimation->setEndValue(pChecked ? pNormalBackgroundColor : pCheckedBackgroundColor);
        pForegroundColorAnimation->setStartValue(pTextCacheColor);
        pForegroundColorAnimation->setEndValue(pChecked ? pNormalForegroundColor : pCheckedForegroundColor);
        pAnimationGroup->start();
    }
}

void NeoToggleButtonPrivate::drawBackground(QPainter *painter)
{
    NEO_PRIVATE_POINT(NeoToggleButton)

    const int borderRadius = 5;
    painter->save();
    painter->setPen(pIsAnimationEnabled ? pBackgroundCacheColor : pChecked ? pCheckedBackgroundColor
                                                                           : pNormalBackgroundColor);
    painter->setBrush(pIsAnimationEnabled ? pBackgroundCacheColor : pChecked ? pCheckedBackgroundColor
                                                                             : pNormalBackgroundColor);
    QRect adjustedRect = q->rect().adjusted(NEO_RADIUS_ADJUSTED_VALUES);
    painter->drawRoundedRect(adjustedRect, borderRadius, borderRadius);
    painter->restore();
}

void NeoToggleButtonPrivate::drawIcon(QPainter *painter)
{
    NEO_PRIVATE_POINT(NeoToggleButton)
    int iconSize = qMin(q->height(), q->width()) - 2 * NEO_RADIUS_ADJUSTED_VALUE;
    QPixmap pixmap = NeoIconInstance->getNeoPixmap(pIcon, pIsAnimationEnabled ? pTextCacheColor : pChecked ? pCheckedForegroundColor
                                                                                                           : pNormalForegroundColor,
                                                   iconSize, iconSize, iconSize);
    if (!pixmap.isNull())
    {
        NEO_PRIVATE_POINT(NeoToggleButton)
        painter->save();
        QRect iconRect = q->rect().adjusted(NEO_RADIUS_ADJUSTED_VALUES);
        iconRect.setSize(QSize(iconSize, iconSize));
        iconRect.moveCenter(q->rect().center());
        painter->drawPixmap(iconRect, pixmap);
        painter->restore();
    }
}

void NeoToggleButtonPrivate::drawText(QPainter *painter)
{
    QPixmap pixmap = NeoIconInstance->getNeoPixmap(pIcon);
    if (!pText.isNull() && pixmap.isNull())
    {
        painter->save();
        NEO_PRIVATE_POINT(NeoToggleButton)
        painter->setPen(pIsAnimationEnabled ? pTextCacheColor : pChecked ? pCheckedForegroundColor
                                                                         : pNormalForegroundColor);
        painter->drawText(q->rect(), Qt::AlignCenter, pText);
        painter->restore();
    }
}